
<h1><span class="yiyi-st" id="yiyi-12">numpy.sum</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.sum"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">sum</code><span class="sig-paren">(</span><em>a</em>, <em>axis=None</em>, <em>dtype=None</em>, <em>out=None</em>, <em>keepdims=&lt;class numpy._globals._NoValue&gt;</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/core/fromnumeric.py#L1743-L1848"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">给定轴上的数组元素的总和。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-15">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-16"><strong>a</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-17">总和的要素。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-18"><strong>axis</strong>：无或int或tuple ints，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-19">沿着其执行和的轴或轴。</span><span class="yiyi-st" id="yiyi-20">默认值axis = None将对输入数组的所有元素求和。</span><span class="yiyi-st" id="yiyi-21">如果轴为负，则从最后一个轴计数到第一个轴。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-22"><span class="versionmodified">版本1.7.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-23">如果axis是ints的元组，则对元组中指定的所有轴执行求和，而不是像以前一样对单个轴或所有轴执行求和。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-24"><strong>dtype</strong>：dtype，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-25">返回的数组和累加器元素的累加器的类型。</span><span class="yiyi-st" id="yiyi-26">除非<em class="xref py py-obj">a</em>具有精度低于默认平台整数的整数dtype，因此默认使用<em class="xref py py-obj">a</em>的dtype。</span><span class="yiyi-st" id="yiyi-27">在这种情况下，如果<em class="xref py py-obj">a</em>被签名，则使用平台整数，而如果<em class="xref py py-obj">a</em>是无符号的，则使用与平台整数相同精度的无符号整数。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-28"><strong>out</strong>：ndarray，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-29">用于放置结果的替代输出数组。</span><span class="yiyi-st" id="yiyi-30">它必须具有与预期输出相同的形状，但如果必要，将输出输出值的类型。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-31"><strong>keepdims</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-32">如果设置为True，则缩小的轴在结果中保留为尺寸为1的尺寸。</span><span class="yiyi-st" id="yiyi-33">使用此选项，结果将相对于原始<em class="xref py py-obj">arr</em>正确广播。</span></p>
<p><span class="yiyi-st" id="yiyi-34">如果传递默认值，则<em class="xref py py-obj">keepdims</em>将不会传递到<a class="reference internal" href="numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-obj docutils literal"><span class="pre">ndarray</span></code></a>的子类的<a class="reference internal" href="#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal"><span class="pre">sum</span></code></a></span><span class="yiyi-st" id="yiyi-35">如果子类<a class="reference internal" href="#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal"><span class="pre">sum</span></code></a>方法不实现<em class="xref py py-obj">keepdims</em>，则会引发任何异常。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-36">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-37"><strong>sum_along_axis</strong>：ndarray</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-38">与<em class="xref py py-obj">a</em>形状相同的数组，指定的轴已删除。</span><span class="yiyi-st" id="yiyi-39">如果<em class="xref py py-obj">a</em>是0-d数组，或者如果<em class="xref py py-obj">轴</em>是无，则返回标量。</span><span class="yiyi-st" id="yiyi-40">如果指定输出数组，则返回对<em class="xref py py-obj">out</em>的引用。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-41">也可以看看</span></p>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-42"><a class="reference internal" href="numpy.ndarray.sum.html#numpy.ndarray.sum" title="numpy.ndarray.sum"><code class="xref py py-obj docutils literal"><span class="pre">ndarray.sum</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-43">等效法。</span></dd>
<dt><span class="yiyi-st" id="yiyi-44"><a class="reference internal" href="numpy.cumsum.html#numpy.cumsum" title="numpy.cumsum"><code class="xref py py-obj docutils literal"><span class="pre">cumsum</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-45">数组元素的累积和。</span></dd>
<dt><span class="yiyi-st" id="yiyi-46"><a class="reference internal" href="numpy.trapz.html#numpy.trapz" title="numpy.trapz"><code class="xref py py-obj docutils literal"><span class="pre">trapz</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-47">使用复合梯形法则集合数组值。</span></dd>
</dl>
<p class="last"><span class="yiyi-st" id="yiyi-48"><a class="reference internal" href="numpy.mean.html#numpy.mean" title="numpy.mean"><code class="xref py py-obj docutils literal"><span class="pre">mean</span></code></a>，<a class="reference internal" href="numpy.average.html#numpy.average" title="numpy.average"><code class="xref py py-obj docutils literal"><span class="pre">average</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-49">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-50">当使用整数类型时，算术是模块化的，并且在溢出时不产生错误。</span></p>
<p><span class="yiyi-st" id="yiyi-51">空数组的和为中性元素0：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([])</span>
<span class="go">0.0</span>
</pre></div>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-52">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">])</span>
<span class="go">2.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.7</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">]])</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">array([0, 6])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([1, 5])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-53">如果累加器太小，则发生溢出：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int8</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int8</span><span class="p">)</span>
<span class="go">-128</span>
</pre></div>
</div>
</dd></dl>
